package com.qhs.sys.service.impl;

import com.qhs.sys.constant.TreeView;
import com.qhs.sys.mapper.SysMenuInfoMapper;
import com.qhs.sys.entity.SysMenuInfo;
import com.qhs.sys.service.MenuService;
import com.qhs.framework.utils.DateUtil;
import com.qhs.framework.utils.IDWorker;
import com.qhs.sys.vo.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by xinchao on 2016/11/30 0030.
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private SysMenuInfoMapper sysMenuInfoMapper;
    @Autowired
    private IDWorker idWorker;


    @Override
    public int deleteByPrimaryKey(Long menuId) {
        return sysMenuInfoMapper.deleteByPrimaryKey(menuId);
    }

    @Override
    public int insert(SysMenuInfo record) {
        record.setStatus(1);
        if (record.getMenuType()==0){
            int sort = sysMenuInfoMapper.selectTopSort();
            record.setSort(sort+1);
            record.setParentId(null);
        }else {
            int parentSort = sysMenuInfoMapper.selectParentSort(record.getParentId());
            record.setSort(parentSort);
        }
        record.setMenuId(idWorker.generateId());
        record.setCreateTime(DateUtil.getDefaultDate());
        return sysMenuInfoMapper.insertSelective(record);
    }

    @Override
    public SysMenuInfo selectByPrimaryKey(Long menuId) {
        return sysMenuInfoMapper.selectByPrimaryKey(menuId);
    }

    @Override
    public int updateByPrimaryKeySelective(SysMenuInfo record) {
        return sysMenuInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<SysMenuInfo> findMenuByWhere(SysMenuInfo sysMenuInfo) {
        return sysMenuInfoMapper.findMenuByWhere(sysMenuInfo);
    }


    @Override
    public List<TreeView> findMenuTree(Long roleId) {

        List<TreeView> treeViews = new ArrayList<>();
        List<SysMenuInfo> allList = sysMenuInfoMapper.findMenuByWhere(new SysMenuInfo());
        List<SysMenuInfo> roleList = sysMenuInfoMapper.findMenuByRole(roleId);
        for(SysMenuInfo sysMenuInfo:allList){
            if(sysMenuInfo.getParentId()==null || sysMenuInfo.getParentId()==0) { //说明是一级菜单
                TreeView treeView = new TreeView();
                treeView.setText(sysMenuInfo.getMenuName());
                treeView.setTags(String.valueOf(sysMenuInfo.getMenuId()));
                ArrayList<TreeView> treeViews1 = new ArrayList<>();
                for(SysMenuInfo sysMenuInfo1:allList){
                        if(sysMenuInfo.getMenuId().equals(sysMenuInfo1.getParentId())){
                            TreeView treeView1 = new TreeView();
                            treeView1.setText(sysMenuInfo1.getMenuName());
                            treeView1.setTags(String.valueOf(sysMenuInfo1.getMenuId()));
                            ArrayList<TreeView> treeViews2 = new ArrayList<>();
                            for(SysMenuInfo sysMenuInfo2:allList) {
                                if (sysMenuInfo1.getMenuId().equals(sysMenuInfo2.getParentId())) {
                                    TreeView treeView2 = new TreeView();
                                    treeView2.setText(sysMenuInfo2.getMenuName());
                                    treeView2.setTags(String.valueOf(sysMenuInfo2.getMenuId()));
                                    HashMap<String,Object> obj = new HashMap();
                                    obj.put("checked",isCheck(sysMenuInfo2.getMenuId(),roleList));
                                    treeView2.setState(obj);
                                    treeViews2.add(treeView2);
                                }
                            }
                            treeView1.setNodes(treeViews2);
                            HashMap<String,Object> obj = new HashMap();
                            obj.put("checked",isCheck(sysMenuInfo1.getMenuId(),roleList));
                            treeView1.setState(obj);
                            treeViews1.add(treeView1);
                        }
                }
                treeView.setNodes(treeViews1);
                HashMap<String,Object> obj = new HashMap();
                obj.put("checked",isCheck(sysMenuInfo.getMenuId(),roleList));
                treeView.setState(obj);
                treeViews.add(treeView);
            }

        }
        return treeViews;
    }

    @Override
    public List selectTopMenu() {

        return  sysMenuInfoMapper.selectTopMenu();
    }

    @Override
    public List<MenuVO> findMenuListByUserId(Long userId) {
        List<MenuVO> menuVOs = new ArrayList<>();
        List<SysMenuInfo> menuList = sysMenuInfoMapper.findMenuByUserId(userId);

        for(SysMenuInfo m : menuList) {
            if(m.getParentId()==null || m.getParentId()==0) { //说明是一级菜单

                List<SysMenuInfo> childList = new ArrayList<>();
                for(SysMenuInfo cm : menuList){
                    if(cm.getParentId() != null && cm.getParentId().equals(m.getMenuId())){
                        childList.add(cm);
                    }
                }

                MenuVO pmVO = new MenuVO(m.getMenuId(), m, childList);

                menuVOs.add(pmVO);
            }
        }

        //    Collections.sort(menuVOs);

        return menuVOs;
    }

    @Override
    public List<SysMenuInfo> findResourceListByUserId(Long userId) {
        return sysMenuInfoMapper.findResourceByUserId(userId);
    }

    public boolean isCheck(Long id,List<SysMenuInfo> list){
        for(SysMenuInfo sysMenuInfo:list){
            if(id.equals(sysMenuInfo.getMenuId())){
                return true;
            }
        }
        return false;
    }


}
